package reflectutil

import (
	"reflect"
	"strconv"
	"errors"
)


/**
根据给定类型,转换value类型,转换失败返回错误
int8不常用注释掉了
@param value string 数据
@param tType string 要转换数据类型
 */
func TypeConversion(value, tType string)(reflect.Value, error)  {
	switch tType {

	case "string" :
		return reflect.ValueOf(value), nil
	case "int":
		i,err := strconv.Atoi(value)
		return reflect.ValueOf(i), err
	//case "int8":
	//	i,err := strconv.ParseInt(value, 10, 64)
	//	return reflect.ValueOf(int8(i)),err
	case "int32":
		i,err := strconv.ParseInt(value, 10, 64)
		return reflect.ValueOf(int32(i)),err
	case "int64":
		i,err := strconv.ParseInt(value, 10, 64)
		return reflect.ValueOf(int64(i)),err
	case "float32":
		i,err := strconv.ParseFloat(value, 64)
		return reflect.ValueOf(float32(i)),err
	case "float64":
		i,err := strconv.ParseFloat(value, 64)
		return reflect.ValueOf(i),err

	default:
		return reflect.ValueOf(value), errors.New("未知类型:"+tType)

	}

}

//
//// convert any numeric value to int64
//func ToInt64(value interface{}) (d int64, err error) {
//	val := reflect.ValueOf(value)
//	switch value.(type) {
//	case int, int8, int16, int32, int64:
//		d = val.Int()
//	case uint, uint8, uint16, uint32, uint64:
//		d = int64(val.Uint())
//	default:
//		err = fmt.Errorf("ToInt64 need numeric not `%T`", value)
//	}
//	return
//}